package com.summer.main;

public class TestSynchronized {

	public static void main(String[] args) {
		final Test test = new Test();
		
		Thread thread = new Thread(new Runnable(){
			@Override
			public void run() {
				test.lock();
			}});
		Thread thread1 = new Thread(new Runnable(){
			@Override
			public void run() {
				test.lock();
			}});
		
		Thread thread2 = new Thread(new Runnable(){
			@Override
			public void run() {
				test.unlock();
			}});
		Thread thread3 = new Thread(new Runnable(){
			@Override
			public void run() {
				test.runing();
			}});
		Thread thread4 = new Thread(new Runnable(){
			@Override
			public void run() {
				test.runing();
			}});
		thread.start();
		thread1.start();
//		thread2.start();
		thread3.start();
		thread4.start();
	}
	
	public static class Test{
		private byte[] lock = new byte[0];  // 特殊的instance变量
		
		
		public void lock(){
			try {
				synchronized (lock) {
					System.out.println("lock");
					lock.wait();
					System.out.println("unwait");
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		public void unlock(){
			synchronized (lock) {
				lock.notify();
				System.out.println("unlock");
			}
		}
		
		public void runing(){

			synchronized (lock) {
				for(int a=0;a<20;a++){
					System.out.println(Thread.currentThread().getName() +" = runing");
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		
	}
	
}
